﻿package frEngine.shape3d
{
    import flash.geom.*;

    public class BezierLine extends Object
    {
        public var isStart:Boolean = false;
        public var startTime:Number = 0;
        public var disTime:Number = 0;
        public var total_length:Number = 0;
        private const STEP:int = 70;
        public var P0:Object;
        public var P1:Object;
        public var P2:Object;
        public var P3:Object;
        public var startRM:Matrix3D;
        public var endRm:Matrix3D;
        private static const TOTAL_SIMPSON_STEP:int = 100;

        public function BezierLine(param1:Object, param2:Object, param3:Object, param4:Object, param5:int)
        {
            this.P0 = param1;
            this.P1 = param2;
            this.P2 = param3;
            this.P3 = param4;
            this.total_length = param5;
            return;
        }// end function

        public function beze(param1:Number, param2:Vector3D) : Vector3D
        {
            var _loc_3:* = 1 - param1;
            var _loc_4:* = _loc_3 * _loc_3 * _loc_3;
            var _loc_5:* = 3 * _loc_3 * _loc_3 * param1;
            var _loc_6:* = 3 * _loc_3 * param1 * param1;
            var _loc_7:* = param1 * param1 * param1;
            param2.x = _loc_4 * this.P0.x + _loc_5 * this.P1.x + _loc_6 * this.P2.x + _loc_7 * this.P3.x;
            param2.y = _loc_4 * this.P0.y + _loc_5 * this.P1.y + _loc_6 * this.P2.y + _loc_7 * this.P3.y;
            param2.z = _loc_4 * this.P0.z + _loc_5 * this.P1.z + _loc_6 * this.P2.z + _loc_7 * this.P3.z;
            return param2;
        }// end function

        public function beze_speed(param1:Number) : Number
        {
            var _loc_2:* = 1 - param1;
            var _loc_3:* = -3 * _loc_2 * _loc_2;
            var _loc_4:* = -6 * _loc_2 * param1;
            var _loc_5:* = -3 * param1 * param1;
            var _loc_6:* = _loc_3 * (this.P0.x - this.P1.x) + _loc_4 * (this.P1.x - this.P2.x) + _loc_5 * (this.P2.x - this.P3.x);
            var _loc_7:* = _loc_3 * (this.P0.y - this.P1.y) + _loc_4 * (this.P1.y - this.P2.y) + _loc_5 * (this.P2.y - this.P3.y);
            var _loc_8:* = _loc_3 * (this.P0.z - this.P1.z) + _loc_4 * (this.P1.z - this.P2.z) + _loc_5 * (this.P2.z - this.P3.z);
            return Math.sqrt(_loc_6 * _loc_6 + _loc_7 * _loc_7 + _loc_8 * _loc_8);
        }// end function

        public function beze_length(param1:Number) : Number
        {
            var _loc_2:* = int(TOTAL_SIMPSON_STEP * param1);
            if (_loc_2 & 1)
            {
                _loc_2++;
            }
            if (_loc_2 == 0)
            {
                return 0;
            }
            var _loc_3:* = _loc_2 / 2;
            var _loc_4:Number = 0;
            var _loc_5:Number = 0;
            var _loc_6:* = param1 / _loc_2;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_3)
            {
                
                _loc_4 = _loc_4 + this.beze_speed((2 * _loc_7 + 1) * _loc_6);
                _loc_7++;
            }
            _loc_7 = 1;
            while (_loc_7 < _loc_3)
            {
                
                _loc_5 = _loc_5 + this.beze_speed(2 * _loc_7 * _loc_6);
                _loc_7++;
            }
            return (this.beze_speed(0) + this.beze_speed(1) + 2 * _loc_5 + 4 * _loc_4) * _loc_6 / 3;
        }// end function

        public function beze_even(param1:Number) : Number
        {
            var _loc_4:Number = NaN;
            var _loc_2:* = param1 * this.total_length;
            var _loc_3:* = param1;
            var _loc_5:int = 0;
            do
            {
                
                _loc_4 = _loc_3 - (this.beze_length(_loc_3) - _loc_2) / this.beze_speed(_loc_3);
                if (Math.abs(_loc_3 - _loc_4) < 0.001 || _loc_5 > 100)
                {
                    break;
                }
                _loc_5++;
                _loc_3 = _loc_4;
            }while (true)
            return _loc_4;
        }// end function

    }
}
